/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;

using Constants = DotNetNuke.Modules.Articles.Common.Constants;
using DotNetNuke.Modules.Articles.Common;
using DotNetNuke.Modules.Articles.Framework;

#endregion

namespace DotNetNuke.Modules.Articles.UI.Controls
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Contents skin object displays the contents of a section.
    /// </summary>
    /// <history>
    /// 	[cnurse]	03/21/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    ToolboxData("<{0}:Contents runat=\"server\"> </{0}:Contents>")]
    public class Contents : CollectionSkinObjectBase
    {
        #region Private Members

        private TableItemStyle _AlternatingItemStyle;
        private TableItemStyle _ItemStyle;
        private TableItemStyle _SelectedItemStyle;

        private static readonly object EventAddArticle = new object();
        private static readonly object EventEditArticle = new object();
        private static readonly object EventArticleSelected = new object();


        #endregion

        #region Protected Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the articles to display
        /// </summary>
        /// <history>
        /// 	[cnurse]	03/21/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected List<ArticleInfo> DisplayArticles
        {
            get
            {
                List<ArticleInfo> _Articles = new List<ArticleInfo>();

                if (DesignMode)
                {
                    ArticleInfo article;
                    for (int i = 0; i < 5; i++)
                    {
                        article = new ArticleInfo();
                        article.Title = "Article " + i.ToString();
                        _Articles.Add(article);
                    }
                }
                else
                {
                    if (StartIndex > 0)
                    {
                        for (int i = StartIndex; i < Articles.Count; i++)
                            _Articles.Add(Articles[i]);
                    }
                    else
                        _Articles = Articles;
                }
                return _Articles;
            }
        }

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Style to apply to the alternating items
        /// </summary>
        /// <history>
        /// 	[cnurse]	03/21/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Category("Appearance"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty),
        NotifyParentProperty(true),
        TypeConverter(typeof(ExpandableObjectConverter)),
        Description("The Style to apply to the alternating items.")]
        public TableItemStyle AlternatingItemStyle
        {
            get 
            {
                if (_AlternatingItemStyle == null)
                    _AlternatingItemStyle = new TableItemStyle();
                if (IsTrackingViewState)
                {
                    ((IStateManager)_AlternatingItemStyle).TrackViewState();
                }
                return _AlternatingItemStyle; 
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Style to apply to the items
        /// </summary>
        /// <history>
        /// 	[cnurse]	03/21/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Category("Appearance"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty),
        NotifyParentProperty(true),
        TypeConverter(typeof(ExpandableObjectConverter)),
        Description("The Style to apply to the items.")]
        public TableItemStyle ItemStyle
        {
            get
            {
                if (_ItemStyle == null)
                    _ItemStyle = new TableItemStyle();
                if (IsTrackingViewState)
                {
                    ((IStateManager)_ItemStyle).TrackViewState();
                }
                return _ItemStyle;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Style to apply to the selected item
        /// </summary>
        /// <history>
        /// 	[cnurse]	03/21/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Category("Appearance"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty),
        NotifyParentProperty(true),
        TypeConverter(typeof(ExpandableObjectConverter)),
        Description("The Style to apply to the selected item.")]
        public TableItemStyle SelectedItemStyle
        {
            get
            {
                if (_SelectedItemStyle == null)
                    _SelectedItemStyle = new TableItemStyle();
                if (IsTrackingViewState)
                {
                    ((IStateManager)_SelectedItemStyle).TrackViewState();
                }
                return _SelectedItemStyle;
            }
        }

        #endregion

        #region Private Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderArticle renders a single Article
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <param name="article">The article to Render</param>
        /// <param name="rowIndex">The index of the Article in the collection</param>
        /// <history>
        /// 	[cnurse]	03/18/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void RenderArticle(HtmlTextWriter writer, ArticleInfo article, int rowIndex)
        {
            bool isSelected = false;
            if ((this.Article != null) && (article.ArticleId == this.Article.ArticleId))
            {
                isSelected = true;
            }

            //Render style
            TableItemStyle style = _ItemStyle;
            if (isSelected)
            {
                style.CopyFrom(_SelectedItemStyle);
            }
            else
            {
                TableItemStyle altStyle = _ItemStyle;
                if (_AlternatingItemStyle != null)
                {
                    altStyle = new TableItemStyle();
                    altStyle.CopyFrom(_ItemStyle);
                    altStyle.CopyFrom(_AlternatingItemStyle);
                }
                if (((rowIndex + 1) % 2) == 0)
                    style = altStyle;
            }

            if (style !=null) 
                style.AddAttributesToRender(writer);

            //Render the TableRow
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            //Render the TableCell
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            //Render Edit Button
            if (this.ParentArticlesModule.IsEditMode && isSelected)
                RenderViewButton(writer, ParentArticlesModule.ArticleURL(TabId, article.ArticleId, Null.NullInteger, Constants.KEY_ViewArticle, true));
            else
                RenderEditButton(writer, ParentArticlesModule.ArticleURL(TabId, article.ArticleId, Null.NullInteger, Constants.KEY_EditArticle, true));

            //Display Article Title
            if (isSelected)
            {
                //Selected Item
                writer.Write(article.Title);
            }
            else
            {
                //Render Hyperlink
                writer.AddAttribute(HtmlTextWriterAttribute.Href, ParentArticlesModule.ArticleURL(TabId, article.ArticleId, Null.NullInteger, Constants.KEY_ViewArticle, true));
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.Write(article.Title);

                //Render end of Hyperlink
                writer.RenderEndTag();
            }

            //Render end of TableCell
            writer.RenderEndTag();

            //Render end of TableRow
            writer.RenderEndTag();

        }

        #endregion

        #region Protected Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderContents renders the contents of the control
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	03/18/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            //Render the Caption
            RenderCaption(writer);

            //Render the Table
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            //Render a row for each Article
            int rowIndex = 0;
            foreach (ArticleInfo article in DisplayArticles)
            {
                RenderArticle(writer, article, rowIndex);
                rowIndex++;
            }

            //Render end of Table
            writer.RenderEndTag();

            //Display Add Button
            RenderAddButton(writer, Constants.KEY_AddArticle, false);

        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Render renders the control
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	03/31/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void Render(HtmlTextWriter writer)
        {
            if (Page != null)
                Page.VerifyRenderingInServerForm(this);

            base.Render(writer);
        }

        #endregion

    }
}
